<template>
  <div>
    <el-tree
      :data="categoryList"
      node-key="catId"
      :props="defaultProps"
      :expand-on-click-node="false"
      :default-expanded-keys="expandedKeys"
    >
      <span
        class="custom-tree-node"
        slot-scope="{ node, data }"
      >
        <span>{{ node.label }}</span>
        <span>
          <!--
            当分类等级为1级和2级的时候才可追加子分类
            -->
          <el-button
            v-if="data.catLevel<=2"
            type="text"
            size="mini"
            @click="() => append(data)"
          >
            <i class="el-icon-plus"> 追加</i>

          </el-button>
          <el-button
            type="text"
            size="mini"
            @click="() => edit(data)"
          >
            <i class="el-icon-edit"> 修改</i>
          </el-button>
          <!--
            当没有子节点的时候
            才可以删除当前分类
            -->
          <el-button
            v-if="node.childNodes.length==0"
            type="text"
            size="mini"
            @click="() => remove(node, data)"
          >
            <i class="el-icon-delete"> 删除</i>
          </el-button>

        </span>
      </span>
    </el-tree>
    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="category">
        <el-form-item
          label="分类名称"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="category.name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="icon图标"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="category.icon"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="计量单位"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="category.productUnit"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="商品数量"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="category.productCount"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="addOrUpdate()"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 树形菜单绑定的数据 是数组
      categoryList: [],
      // 设置默认树形 子节点的属性名称  标签显示的属性名称
      defaultProps: {
        children: 'childerCategory',
        label: 'name'
      },
      expandedKeys: [],
      dialogFormVisible: false,
      category: {
        catId: 0,
        name: '',
        icon: '',
        productUnit: '',
        productCount: 0,
        parentCid: '',
        catLevel: '',
        showStatus: 1,
        sort: 0
      },
      formLabelWidth: '120px',
      title: ''
    }
  },
  methods: {
// 获取数据列表
    getcategoryList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/product/category/list'),
        method: 'get'
      }).then(({data}) => {
        if (data && data.code === 0) {
          console.log(data)
          this.categoryList = data.data
        } else {
          this.dataList = []
        }
        this.dataListLoading = false
      })
    },
    addOrUpdate () {
      var url = this.category.catId === 0 ? '/product/category/save' : '/product/category/update'
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl(`${url}`),
        method: 'post',
        data: this.$http.adornData(this.category, false)
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getcategoryList()
            }

          })
            // 设置默认展开当前删除节点的父节点
          this.expandedKeys = [ this.category.parentCid ]
          this.category.catId = 0
          this.category.name = ''
          this.category.icon = ''
          this.category.parentCid = ''
          this.category.productUnit = ''
          this.category.productCount = 0
          this.category.sort = 0
          this.category.showStatus = 1
          this.dialogFormVisible = false
        }
        this.dataListLoading = false
      })
    },
    edit (data) {
      this.title = '修改分类'
      this.dialogFormVisible = true
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl(`/product/category/info/${data.catId}`),
        method: 'get'
      }).then(({data}) => {
        if (data && data.code === 0) {
          console.log(data)
          this.category = data.category
        } else {
          this.dataList = []
        }
        this.dataListLoading = false
      })
    },
    append (data) {
      this.title = '新增分类'
      this.dialogFormVisible = true
      this.category.parentCid = data.catId
      this.category.catLevel = data.catLevel * 1 + 1
    },
    // 修改分类之后保存
    // modify () {
    //   this.dataListLoading = true
    //   this.$http({
    //     url: this.$http.adornUrl('/product/category/update'),
    //     method: 'post',
    //     data: this.$http.adornData(this.category, false)
    //   }).then(({data}) => {
    //     if (data && data.code === 0) {
    //       this.$message({
    //         message: '操作成功',
    //         type: 'success',
    //         duration: 1500,
    //         onClose: () => {
    //           this.getcategoryList()
    //         }

    //       })
    //       this.dialogFormVisible = false
    //         // 设置默认展开当前删除节点的父节点
    //       this.expandedKeys = [ this.category.parentCid ]
    //     }
    //     this.dataListLoading = false
    //   })
    // },
    // // 新增分类
    // add () {
    //   this.dataListLoading = true
    //   this.$http({
    //     url: this.$http.adornUrl('/product/category/save'),
    //     method: 'post',
    //     data: this.$http.adornData(this.category, false)
    //   }).then(({data}) => {
    //     if (data && data.code === 0) {
    //       this.$message({
    //         message: '操作成功',
    //         type: 'success',
    //         duration: 1500,
    //         onClose: () => {
    //           this.getcategoryList()
    //         }

    //       })
    //       this.dialogFormVisible = false
    //         // 设置默认展开当前删除节点的父节点
    //       this.expandedKeys = [ this.category.parentCid ]
    //     }
    //     this.dataListLoading = false
    //   })
    // },
    remove (node, data) {
      var catIds = [data.catId]
      this.$confirm(`确定对【${data.name}】删除操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/product/category/delete'),
          method: 'post',
          data: this.$http.adornData(catIds, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getcategoryList()
              }
            })
            // 设置默认展开当前删除节点的父节点
            this.expandedKeys = [node.parent.data.catId]
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {})
    }
  },
  created () {
    this.getcategoryList()
  }

}
</script>

<style>
</style>